package com.bluefox.modules.system.controller;

import com.bluefox.modules.system.criteria.DictDataCriteria;
import com.bluefox.modules.system.criteria.DictTypeCriteria;
import com.bluefox.modules.system.domain.SysDictData;
import com.bluefox.modules.system.service.SysDictDataService;
import com.bluefox.modules.system.service.dto.SysDictDataDto;
import com.bluefox.modules.system.service.dto.SysDictTypeDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (SysDictData)表控制层
 *
 * @author makejava
 * @since 2020-05-21 15:21:11
 */
@RestController
@RequestMapping("/system/sysDictData")
public class SysDictDataController {
    /**
     * 服务对象
     */
    @Resource
    private SysDictDataService sysDictDataService;

    /**
     * 新增字典数据
     */
    @PreAuthorize("@el.check('system:data:add')")
    @PostMapping
    public ResponseEntity add(@Validated @RequestBody SysDictDataDto sysDictType) {

        return new ResponseEntity<>(sysDictDataService.insert(sysDictType), HttpStatus.OK);
    }
    /**
     * 修改字典数据
     */
    @PreAuthorize("@el.check('system:data:edit')")
    @PutMapping
    public ResponseEntity changeStatus(@RequestBody SysDictDataDto sysDictType) {

        return new ResponseEntity<>(sysDictDataService.update(sysDictType), HttpStatus.OK);
    }

    /**
     * 删除字典
     */
    @PreAuthorize("@el.check('system:data:remove')")
    @DeleteMapping("/{ids}")
    public ResponseEntity<Object> remove(@PathVariable Long[] ids) {
        sysDictDataService.deleteByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PreAuthorize("@el.check('system:data:list')")
    @GetMapping("/list")
    public ResponseEntity<Object> list(DictDataCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(sysDictDataService.getAll(criteria,pageable), HttpStatus.OK);
    }
    @PreAuthorize("@el.check('system:data:list')")
    @GetMapping("/findAll")
    public ResponseEntity<Object> findAll() {

        return new ResponseEntity<>(sysDictDataService.getAll(), HttpStatus.OK);
    }


    /**
     * 查询字典数据详细
     * @param id
     * @return
     */
    @PreAuthorize("@el.check('system:dict:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity getInfo(@PathVariable Long id) {

        return new ResponseEntity(sysDictDataService.findById(id), HttpStatus.OK);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/dictType/{dictType}")
    public ResponseEntity dictType(@PathVariable String dictType) {


        return new ResponseEntity(sysDictDataService.selectDictDataByType(dictType), HttpStatus.OK);
    }
}
